package test;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class StreamVsOrigin {

    static Random r=new Random();
    @Test
    public void test2(){
        int n= (int) 1e7;
        compare(n);
    }

    public void compare(int n){
        int[] src=new int[n];
        for (int i = 0; i < n; i++) {
            src[i]=r.nextInt(n);
        }
        int[] a1 = src.clone();
        int[] a2 = src.clone();
        long s = System.currentTimeMillis();
        int[] agg1 = agg1(a1);
        long e = System.currentTimeMillis();
        System.out.println("stream："+(e-s));

        s=System.currentTimeMillis();
        int[] agg2 = agg2(a2);
        e=System.currentTimeMillis();
        System.out.println("手写："+(e-s));

//        System.out.println(Arrays.toString(agg1));
//        System.out.println(Arrays.toString(agg2));
    }

    public int[] agg1(int[] arr){
        int[] clone = arr.clone();
        List<String> list=null;
//        Arrays.sort(clone);
        int[] array = Arrays.stream(clone).distinct().toArray();
        return array;
    }
    public int[] agg2(int[] arr){
        int[] clone = arr.clone();
        Arrays.sort(clone);
        int cnt=1;
        for (int i = 1; i < clone.length; i++) {
            if (clone[i]!=clone[cnt-1]){
                clone[cnt++]=clone[i];
            }
        }
        return clone;
    }
}
